package com.work;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

import java.util.Arrays;

public class Aspect {
    public void before(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        System.out.println("AOP日志记录：前置通知......" + methodName + Arrays.toString(args));
    }

    public void after(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        System.out.println("AOP日志记录：后置通知......" + methodName + Arrays.toString(args));
    }

    public void afterReturn(JoinPoint joinPoint, Object resultValue){
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        System.out.println("AOP日志记录：返回通知......" + methodName + Arrays.toString(args));
        System.out.println("方法的返回值为：" + resultValue);
    }

    public void afterThrow(JoinPoint joinPoint, Exception e){
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        System.out.println("AOP日志记录：异常通知......" + methodName + Arrays.toString(args));
        System.out.println("方法抛出的异常对象：" + e);
    }

    public void around(ProceedingJoinPoint proceedingJoinPoint){
        System.out.println("AOP日志记录：环绕通知之前置......");
        try {
            Object resultValue = proceedingJoinPoint.proceed();
            System.out.println("AOP日志记录：环绕通知之返回......");
        } catch (Throwable throwable) {
            System.out.println("AOP日志记录：环绕通知之异常......");
            throwable.printStackTrace();
        }
        System.out.println("AOP日志记录：环绕通知之后置......");
    }
}
